package com.hw.frame.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ImageView;
/**
 * 
* @ClassName: LabeImageView 
* @Description: 带标签的ImageView
* @author hhx518
* @date 2015年4月20日 下午2:33:16 
*
*
*=================================使用方法=======================
*LabeImageView imageView =(LabeImageView) view.findViewById(R.id.iv_car);
 imageView.setLabeText("new")
.setLabeWidth(30)
.setDistance(20)
.setLabeGravity(Gravity.RIGHT_TOP)
.build();
 */


public class LabeImageView  extends ImageView {
	
	public LabeImageView(Context context) {
            super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
             mWidth = getMeasuredWidth();
             mLabeRect = new Rect(0, 0, mWidth, labeWidth);

    }
    public LabeImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if(drawLabe){
              
            	drawLabe(canvas);
            }
    }
    
    private int DEFULT_LabeBackgroundColor = 0xffE91E63;
    private int DEFULT_LabeTextColor = 0xffffffff;
    private int DEFULT_LABEWIDTH = 30;
    private int DEFULT_TEXTSIZE = 17;
    private int DEFULT_DISTANCE = 20;
    private int mWidth;
    private int distance;
    private Rect mLabeRect;
    private int mLabeBackgroundColor = DEFULT_LabeBackgroundColor;
    /** 旋转角度，默认45度 */
    private float _angel;
    private int labeWidth;
    private String labeText ;
    private int textSize ;
    private int textColor = DEFULT_LabeTextColor;
    private Paint mLabePaint;
    
    private boolean drawLabe = false;

    
    private void init() {
            mLabePaint = new Paint();
            mLabePaint.setTextAlign(Align.CENTER);
            mLabePaint.setAntiAlias(true);
            
            distance =(int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, DEFULT_DISTANCE, getResources()
                            .getDisplayMetrics());
            labeWidth = (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, DEFULT_LABEWIDTH, getResources()
                                            .getDisplayMetrics());
            textSize = (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_SP, DEFULT_TEXTSIZE, getResources()
                            .getDisplayMetrics());
            mGravity = Gravity.LEFT_TOP;
    }
    private Gravity mGravity;
    
    public enum Gravity {
            LEFT_TOP, RIGHT_TOP
    }

    public void removeLabe()
    {
            drawLabe = false;
            invalidate();
    }
    private void drawLabe(Canvas canvas)
    {
            canvas.save();
            int pointX  = 0;
            int pointY  = 0;
            calcAngel(mGravity);
            
            switch (mGravity) {
            case LEFT_TOP:
                    //计算真正旋转的坐标点
                    pointX = (int) ((distance+mLabeRect.centerY())/1.414);
                    pointY = pointX;
                    break;

            default:
                    pointX = mWidth -(int) ((distance+mLabeRect.centerY())/1.414);
                    pointY = (int) ((distance+mLabeRect.centerY())/1.414);
                    break;
            }
            //注意，translate，rotate会使坐标轴值发生变化
            canvas.translate(pointX - mLabeRect.centerX(),
                            pointY - mLabeRect.centerY());
            canvas.rotate(_angel, mLabeRect.centerX(), mLabeRect.centerY());
            
            mLabePaint.setColor(mLabeBackgroundColor);

            canvas.drawRect(mLabeRect, mLabePaint);
            mLabePaint.setColor(textColor);
            mLabePaint.setTextSize(textSize);
            FontMetricsInt fontMetrics = mLabePaint.getFontMetricsInt();
            int baseline = mLabeRect.top
                            + (mLabeRect.bottom - mLabeRect.top - fontMetrics.bottom + fontMetrics.top)
                            / 2 - fontMetrics.top;
            // 下面这行是实现水平居中，drawText对应改为传入targetRect.centerX()
            mLabePaint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText(labeText, mLabeRect.centerX(), baseline, mLabePaint);
            canvas.restore();
    }
    
    
    public LabeImageView setLabeBackgroundColor(int color)
    {
            mLabeBackgroundColor = color;
            return this;
    }
    
    public LabeImageView setLabeBackgroundResColor(int resid)
    {
            mLabeBackgroundColor = getResources().getColor(resid);
            return this;
    }
    
    public LabeImageView setLabeText(String text)
    {
            labeText = text;
            return this;
    }
    public LabeImageView setLabeResText(int resid)
    {
            labeText = getResources().getString(resid);
            return this;
    }
    
    public LabeImageView setLabeGravity(Gravity gravity)
    {
            mGravity = gravity;
            return this;
    }
    public LabeImageView setTextColor(int color)
    {
            textColor = color;
            return this;
    }
    /**
     * @param distance dp
     * @return
     */ 
    public LabeImageView setDistance(int distance)
    {
            this.distance =dip2Px(distance);
            return this;
    }
    public LabeImageView setTextResColor(int resid)
    {
            textColor = getResources().getColor(resid);
            return this;
    }
    /**
     * @param width dp
     * @return
     */
    public LabeImageView setLabeWidth(int width)
    {
            labeWidth = dip2Px(width);
            return this;
    }
    public void build()
    {
            drawLabe= true;
            invalidate();
    }
    private void calcAngel(Gravity gravity) {
            switch (gravity) {
            case LEFT_TOP:
                    _angel = -45;
                    break;

            default:
                    _angel = 45;
                    break;
            }
    
    }

    private int dip2Px(float dip) {
            return (int) (dip* getContext().getResources().getDisplayMetrics().density + 0.5f);
    }
    
    
    
    
    /**
     * 使用调用
     * your_view.setLabeText(position+"")
                .setLabeWidth(30)
                .setDistance(20)
                .setLabeGravity(Gravity.RIGHT_TOP)
                .build();
     */
}